home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-2.iso / os2 / rsynth1.zip / phtoelm.c < prev    next >
C/C++ Source or Header  |  1994-11-08  |  3KB  |  160 lines

  1. #include <config.h>
  2.  
  3. /* $Id: phtoelm.c,v 1.13 1994/11/08 13:30:50 a904209 Exp a904209 $
  4.  */
  5. char *phtoelm_id = "$Id: phtoelm.c,v 1.13 1994/11/08 13:30:50 a904209 Exp a904209 $";
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #if defined (__STDC__)
  9. #include <stdarg.h>
  10. #else
  11. #include <varargs.h>
  12. #endif
  13. #include <useconfig.h>
  14. #include "proto.h"
  15. #include "elements.h"
  16. #include "phfeat.h"
  17. #include "darray.h"
  18. #include "trie.h"
  19. #include "phtoelm.h"
  20. #include "hplay.h"
  21. #include "holmes.h"
  22. #include "nsynth.h"
  23.  
  24.  
  25.  
  26. trie_ptr phtoelm = NULL;
  27.  
  28. static Elm_ptr find_elm PROTO((char *s));
  29.  
  30. static Elm_ptr
  31. find_elm(s)
  32. char *s;
  33. {
  34.  Elm_ptr e = Elements;
  35.  while (e < Elements + num_Elements)
  36.   {
  37.    if (!strcmp(s, e->name))
  38.     {
  39.      return e;
  40.     }
  41.    e++;
  42.   }
  43.  return NULL;
  44. }
  45.  
  46. #if defined (__STDC__)
  47. static void
  48. enter(char *p,...)
  49. #else
  50. static void
  51. enter(p, va_alist)
  52. char *p;
  53. va_dcl
  54. #endif
  55. {
  56.  va_list ap;
  57.  char *s;
  58.  char buf[20];
  59.  char *x = buf + 1;
  60. #if defined(__STDC__)
  61.  va_start(ap, p);
  62. #else
  63.  va_start(ap);
  64. #endif
  65.  while ((s = va_arg(ap, char *)))
  66.   {
  67.    Elm_ptr e = find_elm(s);
  68.    if (e)
  69.     *x++ = (e - Elements);
  70.    else
  71.     {
  72.      fprintf(stderr, "Cannot find %s\n", s);
  73.     }
  74.   }
  75.  va_end(ap);
  76.  buf[0] = (x - buf) - 1;
  77.  x = malloc(buf[0] + 1);
  78.  memcpy(x, buf, buf[0] + 1);
  79.  trie_insert(&phtoelm, p, x);
  80. }
  81.  
  82. static void enter_phonemes
  83. PROTO((void))
  84. {
  85. #include "phtoelm.def"
  86. }
  87.  
  88. int
  89. phone_append(p, ch)
  90. darray_ptr p;
  91. int ch;
  92. {
  93.  char *s = (char *) darray_find(p, p->items);
  94.  *s = ch;
  95.  return ch;
  96. }
  97.  
  98. #if 0
  99. #define StressDur(e,s) ((e->ud + (e->du - e->ud) * s / 3)*speed)
  100. #else
  101. #define StressDur(e,s) (s,((e->du + e->ud)/2)*speed)
  102. #endif
  103.  
  104. unsigned
  105. phone_to_elm(phone, n, elm)
  106. char *phone;
  107. int n;
  108. darray_ptr elm;
  109. {
  110.  int stress = 0;
  111.  char *s = phone;
  112.  unsigned t = 0;
  113.  char *limit = s + n;
  114.  if (!phtoelm)
  115.   enter_phonemes();
  116.  while (s < limit && *s)
  117.   {
  118.    char *e = trie_lookup(&phtoelm, &s);
  119.    if (e)
  120.     {
  121.      int n = *e++;
  122.      while (n-- > 0)
  123.       {
  124.        int x = *e++;
  125.        Elm_ptr p = &Elements[x];
  126.        /* This works because only vowels have ud != du,
  127.           and we set stress just before a vowel
  128.         */
  129.        phone_append(elm, x);
  130.        if (!(p->feat & vwl))
  131.         stress = 0;
  132.        t += phone_append(elm,StressDur(p,stress));
  133.        phone_append(elm, stress);
  134.       }
  135.     }
  136.    else
  137.     {
  138.      char ch = *s++;
  139.      switch (ch)
  140.       {
  141.        case '\'':                /* Primary stress */
  142.         stress = 3;
  143.         break;
  144.        case ',':                 /* Secondary stress */
  145.         stress = 2;
  146.         break;
  147.        case '+':                 /* Tertiary stress */
  148.         stress = 1;
  149.         break;
  150.        case '-':                 /* hyphen in input */
  151.         break;
  152.        default:
  153.         fprintf(stderr, "Ignoring %c in '%.*s'\n", ch, n, phone);
  154.         break;
  155.       }
  156.     }
  157.   }
  158.  return t;
  159. }
  160.